package org.jeecg.modules.demo.definedtool;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.jeecg.common.api.vo.Result;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2022年03月27日 17:21
 */

@Data
public class Paging<T> implements Serializable {

    private Page<T> page;

    public Result<Page<T>> getPaging(List<T> list, Integer pageNo, Integer pageSize){
        if (list.isEmpty()) {
            Page<T> page = new Page<>(pageNo, pageSize);
            Result<Page<T>> result = new Result<Page<T>>();
            page.setSize(pageSize);
            page.setCurrent(pageNo);
            page.setTotal(0);
            page.setPages(1);
            page.setRecords(list);
            this.page = page;
            result.setResult(page);
            result.setSuccess(true);
            return result;
        }
        Page<T> page = new Page<>(pageNo, pageSize);
        int count = list.size();
        List<T> pageList = new ArrayList<>();
//计算当前页第一条数据的下标
        int currId = pageNo > 1 ? (pageSize - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && i < count - currId; i++) {
            pageList.add(list.get(currId + i));
        }
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        page.setTotal(count);
//计算分页总页数
        page.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
        page.setRecords(pageList);
        this.page = page;
        Result<Page<T>> result = new Result<Page<T>>();
        result.setResult(page);
        result.setSuccess(true);
        return result;
    }
}
